home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Multimedia / MPlay / mpeg_play2.0 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-08  |  10.4 KB  |  466 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. #include <stdlib.h>
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "util.h"
  26. #include "nextstep.h"
  27.  
  28. /* Declarations of global variables used. */
  29.  
  30. unsigned int curBits;
  31. int bitOffset;
  32. int bufLength;
  33. unsigned int *bitBuffer;
  34.  
  35. /* Bit masks used by bit i/o operations. */
  36.  
  37. unsigned int nBitMask[] = { 0x00000000, 0x80000000, 0xc0000000, 0xe0000000, 
  38.                 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000, 
  39.                 0xff000000, 0xff800000, 0xffc00000, 0xffe00000, 
  40.                 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000, 
  41.                 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000, 
  42.                 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00, 
  43.                 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0, 
  44.                 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe};
  45.  
  46. unsigned int bitMask[] = {  0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff, 
  47.                 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
  48.                 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
  49.                 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
  50.                 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
  51.                 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
  52.                 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
  53.                 0x0000000f, 0x00000007, 0x00000003, 0x00000001};
  54.  
  55. unsigned int rBitMask[] = { 0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8, 
  56.                 0xfffffff0, 0xffffffe0, 0xffffffc0, 0xffffff80, 
  57.                 0xffffff00, 0xfffffe00, 0xfffffc00, 0xfffff800, 
  58.                 0xfffff000, 0xffffe000, 0xffffc000, 0xffff8000, 
  59.                 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000, 
  60.                 0xfff00000, 0xffe00000, 0xffc00000, 0xff800000, 
  61.                 0xff000000, 0xfe000000, 0xfc000000, 0xf8000000, 
  62.                 0xf0000000, 0xe0000000, 0xc0000000, 0x80000000};
  63.  
  64. unsigned int bitTest[] = {  0x80000000, 0x40000000, 0x20000000, 0x10000000, 
  65.                 0x08000000, 0x04000000, 0x02000000, 0x01000000,
  66.                 0x00800000, 0x00400000, 0x00200000, 0x00100000,
  67.                 0x00080000, 0x00040000, 0x00020000, 0x00010000,
  68.                 0x00008000, 0x00004000, 0x00002000, 0x00001000,
  69.                 0x00000800, 0x00000400, 0x00000200, 0x00000100,
  70.                 0x00000080, 0x00000040, 0x00000020, 0x00000010,
  71.                 0x00000008, 0x00000004, 0x00000002, 0x00000001};
  72.  
  73.  
  74. /*
  75.  *--------------------------------------------------------------
  76.  *
  77.  * correct_underflow --
  78.  *
  79.  *    Called when buffer does not have sufficient data to 
  80.  *      satisfy request for bits.
  81.  *      Calls get_more_data, an application specific routine
  82.  *      required to fill the buffer with more data.
  83.  *
  84.  * Results:
  85.  *      None really.
  86.  *  
  87.  * Side effects:
  88.  *    buf_length and buffer fields in curVidStream structure
  89.  *      may be changed.
  90.  *
  91.  *--------------------------------------------------------------
  92.  */
  93.  
  94. void 
  95. correct_underflow() {
  96.  
  97.   int status;
  98.  
  99.   status = get_more_data(curVidStream->buf_start,
  100.              curVidStream->max_buf_length,
  101.              &bufLength, &bitBuffer);
  102.  
  103.   if (status  < 0) {
  104.     if (!quietFlag) {
  105.       fprintf (stderr, "\n");
  106.       perror("Unexpected read error.");
  107.     }
  108.     exit(1);
  109.   }
  110.   else if ((status == 0) && (bufLength < 1)) {
  111.     if (!quietFlag) {
  112.       fprintf(stderr, "\nImproper or missing sequence end code.\n");
  113.     }
  114.     holdFrame = 4; doRewind = 1;
  115.     ExecuteDisplay(curVidStream);
  116.     return;
  117.  
  118. #ifdef ANALYSIS
  119.     PrintAllStats();
  120. #endif
  121.     if (!quietFlag) {
  122.       PrintTimeInfo();
  123.     }
  124.  
  125.     if (loopFlag) longjmp(env, 1);
  126.     DestroyVidStream(curVidStream);
  127.     exit(0);
  128.   }
  129. #ifdef UTIL2
  130.   curBits = *bitBuffer << bitOffset;
  131. #else
  132.   curBits = *bitBuffer;
  133. #endif
  134.  
  135. }
  136.  
  137.  
  138. /*
  139.  *--------------------------------------------------------------
  140.  *
  141.  * next_bits --
  142.  *
  143.  *    Compares next num bits to low order position in mask.
  144.  *      Buffer pointer is NOT advanced.
  145.  *
  146.  * Results:
  147.  *    TRUE, FALSE, or error code.
  148.  *
  149.  * Side effects:
  150.  *    None.
  151.  *
  152.  *--------------------------------------------------------------
  153.  */
  154.  
  155. int next_bits(num, mask)
  156. int num;
  157. unsigned int mask;
  158. {
  159.   unsigned int stream;
  160.   int ret_value;
  161.  
  162.   /* If no current stream, return error. */
  163.  
  164.   if (curVidStream == NULL)
  165.     return NO_VID_STREAM;
  166.  
  167.   /* Get next num bits, no buffer pointer advance. */
  168.  
  169.   show_bitsn(num, stream);
  170.  
  171.   /* Compare bit stream and mask. Set return value toTRUE if equal, FALSE if
  172.      differs. 
  173.   */
  174.  
  175.   if (mask == stream) {
  176.     ret_value = TRUE;
  177.   } else ret_value = FALSE;
  178.  
  179.   /* Return return value. */
  180.  
  181.   return ret_value;
  182. }
  183.  
  184.  
  185. /*
  186.  *--------------------------------------------------------------
  187.  *
  188.  * get_ext_data --
  189.  *
  190.  *    Assumes that bit stream is at begining of extension
  191.  *      data. Parses off extension data into dynamically 
  192.  *      allocated space until start code is hit. 
  193.  *
  194.  * Results:
  195.  *    Pointer to dynamically allocated memory containing
  196.  *      extension data.
  197.  *
  198.  * Side effects:
  199.  *    Bit stream irreversibly parsed.
  200.  *
  201.  *--------------------------------------------------------------
  202.  */
  203.  
  204. char *get_ext_data ()
  205. {
  206.   int size, marker;
  207.   char *dataPtr;
  208.   unsigned int data;
  209.  
  210.   /* Set initial ext data buffer size. */
  211.  
  212.   size = EXT_BUF_SIZE;
  213.  
  214.   /* Allocate ext data buffer. */
  215.  
  216.   dataPtr = (char *) malloc(size);
  217.  
  218.   /* Initialize marker to keep place in ext data buffer. */
  219.  
  220.   marker = 0;
  221.  
  222.   /* While next data is not start code... */
  223.   while (!next_bits(24, 0x000001)) {
  224.  
  225.     /* Get next byte of ext data. */
  226.  
  227.     get_bits8(data);
  228.  
  229.     /* Put ext data into ext data buffer. Advance marker. */
  230.  
  231.     dataPtr[marker] = (char) data;
  232.     marker++;
  233.  
  234.     /* If end of ext data buffer reached, resize data buffer. */
  235.  
  236.     if (marker == size) {
  237.       size += EXT_BUF_SIZE;
  238.       dataPtr = (char *) realloc(dataPtr, size);
  239.     }
  240.   }
  241.  
  242.   /* Realloc data buffer to free any extra space. */
  243.  
  244.   dataPtr = (char *) realloc(dataPtr, marker);
  245.  
  246.   /* Return pointer to ext data buffer. */
  247.  
  248.   return dataPtr;
  249. }
  250.  
  251.  
  252. /*
  253.  *--------------------------------------------------------------
  254.  *
  255.  * next_start_code --
  256.  *
  257.  *    Parses off bitstream until start code reached. When done
  258.  *      next 4 bytes of bitstream will be start code. Bit offset
  259.  *      reset to 0.
  260.  *
  261.  * Results:
  262.  *    Status code.
  263.  *
  264.  * Side effects:
  265.  *    Bit stream irreversibly parsed.
  266.  *
  267.  *--------------------------------------------------------------
  268.  */
  269.  
  270. int next_start_code()
  271. {
  272.   int state;
  273.   int byteoff;
  274.   unsigned int data;
  275.  
  276.   /* If no current stream, return error. */
  277.  
  278.   if (curVidStream == NULL)
  279.     return NO_VID_STREAM;
  280.  
  281.   /* If insufficient buffer length, correct underflow. */
  282.  
  283.   if (bufLength < 2) {
  284.     correct_underflow();
  285.   }
  286.  
  287.   /* If bit offset not zero, reset and advance buffer pointer. */
  288.  
  289.   byteoff = bitOffset % 8;
  290.  
  291.   if (byteoff != 0) {
  292.     flush_bits((8-byteoff));
  293.   }
  294.  
  295.   /* Set state = 0. */
  296.  
  297.   state = 0;
  298.  
  299.   /* While buffer has data ... */
  300.  
  301.   while(bufLength > 0) {
  302.  
  303.     /* If insufficient data exists, correct underflow. */
  304.  
  305.     if (bufLength < 2) {
  306.       correct_underflow();
  307.     }
  308.  
  309.     /* If next byte is zero... */
  310.  
  311.     get_bits8(data);
  312.  
  313.     if (data == 0) {
  314.  
  315.       /* If state < 2, advance state. */
  316.  
  317.       if (state < 2) state++;
  318.     }
  319.  
  320.     /* If next byte is one... */
  321.  
  322.     else if (data == 1) {
  323.  
  324.       /* If state == 2, advance state (i.e. start code found). */
  325.  
  326.       if (state == 2) state++;
  327.  
  328.       /* Otherwise, reset state to zero. */
  329.  
  330.       else state = 0;
  331.     }
  332.  
  333.     /* Otherwise byte is neither 1 or 0, reset state to 0. */
  334.  
  335.     else {
  336.       state = 0;
  337.     }
  338.  
  339.     /* If state == 3 (i.e. start code found)... */
  340.  
  341.     if (state == 3) {
  342.  
  343.       /* Set buffer pointer back and reset length & bit offsets so
  344.      next bytes will be beginning of start code. 
  345.       */
  346.  
  347.       bitOffset = bitOffset - 24;
  348.  
  349. #ifdef ANALYSIS
  350.       bitCount -= 24;
  351. #endif
  352.  
  353.       if (bitOffset < 0) {
  354.     bitOffset = 32 + bitOffset;
  355.     bufLength++;
  356.     bitBuffer--;
  357. #ifdef UTIL2
  358.     curBits = *bitBuffer << bitOffset;
  359. #else
  360.     curBits = *bitBuffer;
  361. #endif
  362.       }
  363.       else {
  364. #ifdef UTIL2
  365.     curBits = *bitBuffer << bitOffset;
  366. #else
  367.     curBits = *bitBuffer;
  368. #endif
  369.       }
  370.  
  371.       /* Return success. */
  372.  
  373.       return OK;
  374.     }
  375.   }
  376.  
  377.   /* Return underflow error. */
  378.  
  379.   return UNDERFLOW;
  380. }
  381.  
  382.  
  383. /*
  384.  *--------------------------------------------------------------
  385.  *
  386.  * get_extra_bit_info --
  387.  *
  388.  *    Parses off extra bit info stream into dynamically 
  389.  *      allocated memory. Extra bit info is indicated by
  390.  *      a flag bit set to 1, followed by 8 bits of data.
  391.  *      This continues until the flag bit is zero. Assumes
  392.  *      that bit stream set to first flag bit in extra
  393.  *      bit info stream.
  394.  *
  395.  * Results:
  396.  *    Pointer to dynamically allocated memory with extra
  397.  *      bit info in it. Flag bits are NOT included.
  398.  *
  399.  * Side effects:
  400.  *    Bit stream irreversibly parsed.
  401.  *
  402.  *--------------------------------------------------------------
  403.  */
  404.  
  405. char *get_extra_bit_info ()
  406. {
  407.   int size, marker;
  408.   char *dataPtr;
  409.   unsigned int data;
  410.  
  411.   /* Get first flag bit. */
  412.   get_bits1(data);
  413.  
  414.   /* If flag is false, return NULL pointer (i.e. no extra bit info). */
  415.  
  416.   if (!data) return NULL;
  417.  
  418.   /* Initialize size of extra bit info buffer and allocate. */
  419.  
  420.   size = EXT_BUF_SIZE;
  421.   dataPtr = (char *) malloc(size);
  422.  
  423.   /* Reset marker to hold place in buffer. */
  424.  
  425.   marker = 0;
  426.  
  427.   /* While flag bit is true. */
  428.  
  429.   while (data) {
  430.  
  431.     /* Get next 8 bits of data. */
  432.     get_bits8(data);
  433.  
  434.     /* Place in extra bit info buffer. */
  435.  
  436.     dataPtr[marker] = (char) data;
  437.     marker++;
  438.  
  439.     /* If buffer is full, reallocate. */
  440.  
  441.     if (marker == size) {
  442.       size += EXT_BUF_SIZE;
  443.       dataPtr = (char *) realloc(dataPtr, size);
  444.     }
  445.  
  446.     /* Get next flag bit. */
  447.     get_bits1(data);
  448.   }
  449.  
  450.   /* Reallocate buffer to free extra space. */
  451.  
  452.   dataPtr = (char *) realloc(dataPtr, marker);
  453.  
  454.   /* Return pointer to extra bit info buffer. */
  455.  
  456.   return dataPtr;
  457. }
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.